package com.etime.shycourse.services.account.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.dao.SettlementMapper;
import com.etime.shycourse.dto.AccountHistoryDto;
import com.etime.shycourse.dto.AccountMyDto;
import com.etime.shycourse.dto.AccountUserDto;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.Settlement;
import com.etime.shycourse.pojo.SysUser;
import com.etime.shycourse.services.account.interfaces.AccountMyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AccountMyServiceImpl
 * @Description  我的提成service层接口的实现类
 * @Author huangwei
 * @Date 2022/7/18 16:00
 */
@Service
public class AccountMyServiceImpl implements AccountMyService{

    @Autowired
    private SettlementMapper settlementMapper;

    @Autowired
    private Tools tools;

    /**
     * @param settlementpay 是否支付 0-未支付  1-已支付
     * @param pageNum 第几页
     * @param pageSize 每页大小
     * @return: com.etime.shycourse.dto.AccountUserDto
     * @description: 查询用户未支付的提成总额
     **/
    @Override
    public Page<AccountUserDto> selectAccountSumByPage(
            Integer settlementpay,Long pageNum, Long pageSize,String token) {
        QueryWrapper<AccountUserDto> queryWrapper = new QueryWrapper<>();
        CurrentUser currentUserFromRedis= tools.getCurrentUserFromRedis(token);
        if(currentUserFromRedis != null){
            SysUser user = currentUserFromRedis.getUser();

            queryWrapper.eq(user.getUserid()!=null,"userid", user.getUserid());
            if (user.getIssaler() == 1){
                queryWrapper.eq("issaler", 1);
            }
            if (user.getIsteacher() == 1){
                queryWrapper.eq("isteacher", 1);
            }
        }
        queryWrapper.eq("settlementpay",0);
        Page<AccountUserDto> page = new Page<>(pageNum,pageSize);
            queryWrapper.eq("settlementpay", settlementpay);
        return settlementMapper.selectAccountSumByPage(page,queryWrapper);
    }

    /**
     * @param settlementid 结算对象id
     * @param pageNum  第几页
     * @param pageSize  每页大小
     * @return: com.etime.shycourse.config.PageMessage<com.etime.shycourse.dto.AccountMyDto>
     * @description:  查询未支付的提成明细信息
     **/
    @Override
    public Page<AccountMyDto> selectAccountMyByPage(Integer settlementid, Long pageNum, Long pageSize) {
        Page<AccountMyDto> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AccountMyDto> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("settlementpay", 0);
            queryWrapper.eq("settlementuserid",settlementid);
        return settlementMapper.selectAccountMyByPage(page, queryWrapper);
    }

    /**
     * @param settlementuserid 结算对象id
     * @param time  支付时间
     * @param pageNum 第几页
     * @param pageSize 每页大小
     * @return: com.etime.shycourse.dto.AccountHistoryDto
     * @description: 查询历史提成总览
     **/
    @Override
    public Page<AccountHistoryDto> selectAccountHistory(
            Integer settlementuserid, String time ,Long pageNum,Long pageSize) {
        Page<AccountHistoryDto> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AccountHistoryDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(time!=null&&!"".equalsIgnoreCase(time),"settlementpatytime", time)
                    .eq("settlementpay", 1)
                    .eq(settlementuserid!=null,"settlementuserid", settlementuserid);
        return settlementMapper.selectAccountHistory(page,queryWrapper);
    }

    /**
     * @return: java.util.List<java.lang.String>
     * @description: 统计所有支付时间
     **/
    @Override
    public List<Settlement> selectPaytime(Integer settlementuserid) {
        return settlementMapper.selectPaytime(settlementuserid);

    }

    /**
     * @param time 支付时间
     * @return: java.util.List<com.etime.shycourse.dto.AccountUserDto>
     * @description: 查询每个时间点对应的用户信息
     **/
    @Override
    public List<AccountUserDto> selectUserInfo(String time) {
        QueryWrapper<String> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("settlementpatytime", time)
                    .eq("settlementpay", 1);
        return settlementMapper.selectUserInfo(queryWrapper);
    }

    /**
     * @param settlementid 结算对象id
     * @param time  支付时间
     * @param pageNum  第几页
     * @param pageSize 每页大小
     * @return: com.etime.shycourse.config.PageMessage<com.etime.shycourse.dto.AccountMyDto>
     * @description: 查询历史一层明细信息
     **/
    @Override
    public Page<AccountMyDto> selectHistroyDetails(Integer settlementid,String time, Long pageNum, Long pageSize) {
        Page<AccountMyDto> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AccountMyDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("settlementid", settlementid)
                    .eq("settlementpay", 1)
                    .eq(time!=null&&!"".equalsIgnoreCase(time),"settlementpatytime", time);
        return settlementMapper.selectHistroyDetails(page,queryWrapper);
    }


}








